@echo off
chcp 65001 >nul 2>&1
echo ===================================
echo SmartCity-LLM Trainer 本地启动
echo ===================================
echo.

cd /d "%~dp0"

echo 检查Python环境...
python --version >nul 2>&1
if %errorlevel% neq 0 (
    echo 错误: 未找到Python，请先安装Python 3.8+
    echo 下载地址: https://www.python.org/downloads/
    echo.
    pause
    exit /b 1
)

echo Python环境检查通过
echo.

echo 检查项目文件...
if not exist "backend\main.py" (
    echo 错误: 未找到后端主文件 backend\main.py
    echo 请确保在正确的项目目录中运行此脚本
    echo.
    pause
    exit /b 1
)

echo 项目文件检查通过
echo.
echo 启动选项:
echo 1. 快速启动 (仅后端API + SQLite)
echo 2. Docker Compose 启动 (推荐，需要Docker)
echo 3. 手动配置启动 (完整环境)
echo 4. 检查环境状态
echo.
set /p choice="请选择 (1-4): "

if "%choice%"=="1" goto quick_start
if "%choice%"=="2" goto docker
if "%choice%"=="3" goto manual
if "%choice%"=="4" goto check_env
echo 无效选择，默认使用快速启动
goto quick_start

:quick_start
echo.
echo ==========================================
echo 快速启动模式 (后端API + SQLite)
echo ==========================================
echo.
cd backend

echo 启动快速开发服务器...
echo 如果遇到问题，请查看错误信息
echo.
python quick_start.py
if %errorlevel% neq 0 (
    echo.
    echo 启动失败，可能的原因:
    echo 1. 缺少Python依赖包
    echo 2. Python版本过低 (需要3.8+)
    echo 3. 代码文件有问题
    echo.
    echo 建议运行选项4检查环境状态
    pause
)
goto end

:docker
echo.
echo ==========================================
echo Docker Compose 启动
echo ==========================================
echo.
docker --version >nul 2>&1
if %errorlevel% neq 0 (
    echo 错误: 未找到Docker
    echo 请先安装Docker Desktop: https://www.docker.com/products/docker-desktop/
    echo.
    pause
    exit /b 1
)

docker-compose --version >nul 2>&1
if %errorlevel% neq 0 (
    echo 错误: 未找到Docker Compose
    echo Docker Compose通常包含在Docker Desktop中
    echo.
    pause
    exit /b 1
)

echo Docker环境检查通过
echo.
echo 启动所有服务...
docker-compose up -d
if %errorlevel% neq 0 (
    echo.
    echo Docker启动失败，可能的原因:
    echo 1. Docker服务未运行
    echo 2. 端口被占用
    echo 3. docker-compose.yml文件有问题
    echo.
    echo 请检查Docker Desktop是否正在运行
    pause
    goto end
)

echo.
echo 服务启动完成!
echo ==========================================
echo 可访问的服务:
echo API文档: http://localhost:8000/api/v1/docs
echo 健康检查: http://localhost:8000/health
echo ==========================================
echo.
echo 常用命令:
echo 查看服务状态: docker-compose ps
echo 查看日志: docker-compose logs -f
echo 停止服务: docker-compose down
echo.
pause
goto end

:manual
echo.
echo ==========================================
echo 手动配置启动
echo ==========================================
echo.
cd backend

echo 检查pip...
python -m pip --version >nul 2>&1
if %errorlevel% neq 0 (
    echo 错误: pip不可用
    echo 请重新安装Python并确保包含pip
    pause
    goto end
)

echo 创建虚拟环境...
if not exist "venv" (
    python -m venv venv
    if %errorlevel% neq 0 (
        echo 虚拟环境创建失败
        pause
        goto end
    )
)

echo 激活虚拟环境...
call venv\Scripts\activate.bat
if %errorlevel% neq 0 (
    echo 虚拟环境激活失败
    pause
    goto end
)

echo 安装依赖...
pip install fastapi uvicorn sqlalchemy aiosqlite pydantic python-jose passlib structlog
if %errorlevel% neq 0 (
    echo 依赖安装失败
    pause
    goto end
)

echo.
echo 启动服务...
set DATABASE_URL=sqlite+aiosqlite:///./test.db
set SECRET_KEY=dev-secret-key
set ENVIRONMENT=development

python -m uvicorn main:app --host 0.0.0.0 --port 8000 --reload
goto end

:check_env
echo.
echo ==========================================
echo 环境状态检查
echo ==========================================
echo.

echo [Python环境]
python --version 2>&1
if %errorlevel% neq 0 (
    echo ❌ Python未安装或不在PATH中
) else (
    echo ✅ Python可用
)

echo.
echo [pip状态]
python -m pip --version 2>&1
if %errorlevel% neq 0 (
    echo ❌ pip不可用
) else (
    echo ✅ pip可用
)

echo.
echo [Docker状态]
docker --version >nul 2>&1
if %errorlevel% neq 0 (
    echo ❌ Docker未安装
) else (
    echo ✅ Docker可用
    docker-compose --version >nul 2>&1
    if %errorlevel% neq 0 (
        echo ❌ Docker Compose不可用
    ) else (
        echo ✅ Docker Compose可用
    )
)

echo.
echo [项目文件]
if exist "backend\main.py" (
    echo ✅ 后端主文件存在
) else (
    echo ❌ 后端主文件缺失
)

if exist "backend\requirements.txt" (
    echo ✅ 依赖文件存在
) else (
    echo ❌ 依赖文件缺失
)

if exist "docker-compose.yml" (
    echo ✅ Docker配置文件存在
) else (
    echo ❌ Docker配置文件缺失
)

echo.
echo [Python包检查]
cd backend
echo 检查关键Python包...
python -c "import fastapi; print('✅ FastAPI可用')" 2>nul || echo "❌ FastAPI未安装"
python -c "import uvicorn; print('✅ Uvicorn可用')" 2>nul || echo "❌ Uvicorn未安装"
python -c "import sqlalchemy; print('✅ SQLAlchemy可用')" 2>nul || echo "❌ SQLAlchemy未安装"
python -c "import pydantic; print('✅ Pydantic可用')" 2>nul || echo "❌ Pydantic未安装"

echo.
echo 环境检查完成！
echo.
pause
goto end

:end
echo.
echo 感谢使用 SmartCity-LLM Trainer!
echo 如有问题，请查看项目文档或提交Issue